package com.wc.alorithm_blue_bridge._图论.会面;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/3/27 14:35
 * @description
 * https://www.lanqiao.cn/courses/46292/learning/?id=3440244&compatibility=false
 */
public class Main {
    /**
     * 思路：弗洛伊德 floyd
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110, INF = 0x3f3f3f3f;
    static int[][] dist = new int[N][N];
    static int n, m, s1, e1, s2, e2;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            Arrays.fill(dist[i], INF);
            dist[i][i] = 0;
        }
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            // floyd 细节
            dist[a][b] = dist[b][a] = Math.min(dist[a][b], c);
        }
        // floyd
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
        s1 = sc.nextInt();
        e1 = sc.nextInt();
        s2 = sc.nextInt();
        e2 = sc.nextInt();
        int res = INF;
        // 以 i 设置为中途见面的点
        for (int i = 1; i <= n; i++) {
            res = Math.min(res, Math.max(dist[s1][i], dist[s2][i]) + Math.max(dist[i][e1], dist[i][e2]));
        }
        if (res > INF / 2) res = -1;
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

